// 随机字符串
export function randomStr(len = 16): string {
  const l = Math.max(1, Math.min(100, len))
  let str = '_'

  while (str.length < l) {
    str += Math.random().toString(36).slice(2)
  }

  return str.slice(0, l)
}

// 数值越界限制
export function limit(num = 0, min = -Infinity, max = Infinity): number {
  return Math.max(min, Math.min(max, num))
}

// 数值保留小数点
export function toFixed(num = 0, count = 2): number {
  return parseFloat(num.toFixed(Math.max(0, Math.min(10, count))))
}

// 異步等待
export function sleep(ms = 1000): Promise<void> {
  return new Promise((r) => {
    const t = setTimeout(() => {
      clearTimeout(t)
      r()
    }, ms)
  })
}
